﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace AngelPerez.Taringa.PostEdit {
	public partial class ColorSelectionWindow : Form {
		private static bool _mouseDown = false;
		private Color _selectedColor = Color.Black;

		public Color SelectedColor {
			get {
				return _selectedColor;
			}
		}

		public ColorSelectionWindow() {
			InitializeComponent();
		}

		private void colorWheel_Paint(object sender, PaintEventArgs e) {
			using (Graphics gr = e.Graphics) {
				gr.SmoothingMode = SmoothingMode.HighQuality;

				int xmin = 0;
				int ymin = 0;
				int wid = 128;
				int hgt = 128;
				Rectangle rect = new Rectangle(xmin, ymin, wid, hgt);
				GraphicsPath wheel_path = new GraphicsPath();
				wheel_path.AddEllipse(rect);
				wheel_path.Flatten();

				int num_pts = (wheel_path.PointCount - 1) / 3;
				Color[] surround_colors = new Color[wheel_path.PointCount];
				float r = 255, g = 0, b = 0;
				float dr, dg, db;

				dr = -255 / num_pts;
				db = 255 / num_pts;
				for (int i = 0; i < num_pts; i++) {
					surround_colors[i] = Color.FromArgb(255, (int)r, (int)g, (int)b);
					r += dr;
					b += db;
				}

				r = 0; g = 0; b = 255;
				dg = 255 / num_pts;
				db = -255 / num_pts;
				for (int i = num_pts; i < 2 * num_pts; i++) {
					surround_colors[i] = Color.FromArgb(255, (int)r, (int)g, (int)b);
					g += dg;
					b += db;
				}

				r = 0; g = 255; b = 0;
				dr = 255 / (wheel_path.PointCount - 2 * num_pts);
				dg = -255 / (wheel_path.PointCount - 2 * num_pts);
				for (int i = 2 * num_pts; i < wheel_path.PointCount; i++) {
					surround_colors[i] = Color.FromArgb(255, (int)r, (int)g, (int)b);
					r += dr;
					g += dg;
				}

				using (PathGradientBrush path_brush = new PathGradientBrush(wheel_path)) {
					path_brush.CenterColor = Color.White;
					path_brush.SurroundColors = surround_colors;

					gr.FillPath(path_brush, wheel_path);
					gr.DrawEllipse(new Pen(new SolidBrush(Color.White), 2), rect);
				}
			}
		}

		private void UpdateFields() {
			panel1.BackColor = SelectedColor;

			textBox1.Text = SelectedColor.R.ToString();
			textBox2.Text = SelectedColor.G.ToString();
			textBox3.Text = SelectedColor.B.ToString();
			textBox4.Text = SelectedColor.A.ToString();

			hexBox.Text = SelectedColor.R.ToString("X2") + SelectedColor.G.ToString("X2") + SelectedColor.B.ToString("X2");
		}

		private void colorWheel_MouseMove(object sender, MouseEventArgs e) {
			if (_mouseDown) {
				try {
					Bitmap bitmap = new Bitmap(128, 128);
					Point position = e.Location;
				 	colorWheel.DrawToBitmap(bitmap, new Rectangle(0, 0, 128, 128));
					Color current = bitmap.GetPixel(position.X, position.Y);
					_selectedColor = current;
					UpdateFields();
				} catch {}
			}
		}

		private void colorWheel_MouseUp(object sender, MouseEventArgs e) {
			_mouseDown = false;
		}

		private void colorWheel_MouseDown(object sender, MouseEventArgs e) {
			_mouseDown = true;
			try {
				Bitmap bitmap = new Bitmap(128, 128);
				Point position = e.Location;
				colorWheel.DrawToBitmap(bitmap, new Rectangle(0, 0, 128, 128));
				Color current = bitmap.GetPixel(position.X, position.Y);
				_selectedColor = current;
				UpdateFields();
			} catch { }
		}

		private void panel1_Paint(object sender, PaintEventArgs e) {
			e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(64, 0, 0, 0))), 0, 0, panel1.Width - 1, panel1.Height - 1);
		}

		private void button1_Click(object sender, EventArgs e) {
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void button2_Click(object sender, EventArgs e) {
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Close();
		}
	}
}
